/**
 * Essence - Essence_YouTube
 * 
 * Youtube libraries for parsing and interacting with YouTube feeds
 * Requires jQuery 1.2.1 or Higher
 *
**/

var youtube = {};

youtube.parsePlaylist = function(_feed, _callback)
{
	// Init temporary collection
	var _collection = [];

	// Find and cycle entries
	var entries = $(_feed).find('entry');

	$.each(entries, function(i, val){
	
		// Init temporary _video obj
		var _video = {};
		var error = 0;

		// URL
		if (isDefined($(val).find('media\\:player').attr('url')))
		{
			_video.url = $(val).find('media\\:player').attr('url');
		}
		else if (isDefined($(val).find('player').attr('url')))
		{
			_video.url = $(val).find('player').attr('url');
		}
		else
		{
			error = 1;
		}

		// Title
		if (isDefined($(val).find('title').text()))
		{
			_video.title = $(val).find('title').text();
		}
		else
		{
			error = 1;
		}

		// Description
		if (isDefined($(val).find('media\\:description').text()))
		{
			_video.description = $(val).find('media\\:description').text();
		}
		else if (isDefined($(val).find('description')))
		{
			_video.description = $(val).find('description').text();
		}
		else
		{
			error = 1;
		}

		// Date
		if (isDefined($(val).find('updated').text() != ''))
		{
			_video.date = $(val).find('updated').text();
		}
		else
		{
			error = 1;
		}

		// Author Name && URL
		if (isDefined($(val).find('author')))
		{
			_video.author_name = $(val).find('author').find('name').text();
			_video.author_url = $(val).find('author').find('uri').text();
		}
		else
		{
			error = 1;
		}

		// Rating
		if (isDefined($(val).find('gd\\:rating')))
		{
			_video.rating_average = $(val).find('gd\\:rating').attr('average');
			_video.rating_min = $(val).find('gd\\:rating').attr('min');
			_video.rating_max = $(val).find('gd\\:rating').attr('max');
			_video.rating_ratings = $(val).find('gd\\:rating').attr('numRaters');
		}
		else if (isDefined($(val).find('rating')))
		{
			_video.rating_average = $(val).find('rating').attr('average');
			_video.rating_min = $(val).find('rating').attr('min');
			_video.rating_max = $(val).find('rating').attr('max');
			_video.rating_ratings = $(val).find('rating').attr('numRaters');
		}
		else
		{
			error = 1;
		}

		// View Count
		if (isDefined($(val).find('yt\\:statistics').attr('viewCount')))
		{
			_video.views = $(val).find('yt\\:statistics').attr('viewCount');
		}
		else if (isDefined($(val).find('statistics').attr('viewCount')))
		{
			_video.views = $(val).find('statistics').attr('viewCount');
		}
		else
		{
			error = 1;
		}

		// Duration
		if (isDefined($(val).find('yt\\:duration').attr('seconds')))
		{
			_video.duration = $(val).find('yt\\:duration').attr('seconds');
		}
		else if (isDefined($(val).find('duration').attr('seconds')))
		{
			_video.duration = $(val).find('duration').attr('seconds');
		}
		else
		{
			error = 1;
		}

		// Get video swf
		var url = _video.url;
		if(url)
		{
			_video.VideoSwfUrl = "http://www.youtube.com/v/" + url.split("?v=")[1]
		}
		else
		{
			error = 1;
		}

		// Thumbnail
		if (isDefined($(val).find('media\\:thumbnail').attr('url')))
		{
			_video.thumbnail = $(val).find('media\\:thumbnail').attr('url');
		}
		else if (isDefined($(val).find('thumbnail').attr('url')))
		{
			_video.thumbnail = $(val).find('thumbnail').attr('url');
		}
		else
		{
			error = 1;
		}

		// Push _video obj to collection
		if (error !== 1)
		{
			_collection.push(_video);
		}

	});

	if(_callback)
	{
		_callback(_collection);
	}
	else
	{
		alert('Essence_Youtube - parsePlaylist must have a callback');

		return false;
	}
}

function isDefined(variable)
{
    return (typeof(variable) == "undefined")?  false: true;
}